package com.huanxing.cloud.pay.api.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huanxing.cloud.common.core.desensitization.KeyDesensitization;
import com.huanxing.cloud.common.core.entity.Result;
import com.huanxing.cloud.pay.api.service.IPayConfigService;
import com.huanxing.cloud.pay.common.entity.PayConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 支付配置
 *
 * @author lijx
 * @since 2022/3/18 10:11
 */
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/payconfig")
@Api(value = "payconfig", tags = "支付配置")
public class PayConfigController {

  private final IPayConfigService payConfigService;
  private final KeyDesensitization keyDesensitization = new KeyDesensitization();

  @ApiOperation(value = "支付配置列表")
  @PreAuthorize("hasAuthority('payapi:payconfig:page')")
  @GetMapping("/page")
  public Result page(Page page, PayConfig payConfig) {
    return Result.success(payConfigService.page(page, Wrappers.lambdaQuery(payConfig)));
  }

  @ApiOperation(value = "支付配置查询")
  @PreAuthorize("hasAuthority('payapi:payconfig:get')")
  @GetMapping("/{id}")
  public Result page(@PathVariable("id") String id) {
    return Result.success(payConfigService.getById(id));
  }

  @ApiOperation(value = "支付配置新增")
  @PreAuthorize("hasAuthority('payapi:payconfig:add')")
  @PostMapping
  public Result add(@RequestBody PayConfig payConfig) {
    return Result.success(payConfigService.save(payConfig));
  }

  @ApiOperation(value = "支付配置编辑")
  @PreAuthorize("hasAuthority('payapi:payconfig:edit')")
  @PutMapping
  public Result edit(@RequestBody PayConfig payConfig) {
    if (StrUtil.isBlank(payConfig.getId())) {
      return Result.fail("支付配置ID为空");
    }
    PayConfig target = payConfigService.getById(payConfig.getId());
    if (ObjectUtil.isNull(target)) {
      return Result.fail("支付不存在");
    }

    if (keyDesensitization.serialize(target.getApiv3Key()).equals(payConfig.getApiv3Key())) {
      payConfig.setApiv3Key(null);
    }
    if (keyDesensitization
        .serialize(target.getCertSerialNo())
        .equals(payConfig.getCertSerialNo())) {
      payConfig.setCertSerialNo(null);
    }
    if (keyDesensitization.serialize(target.getMchKey()).equals(payConfig.getMchKey())) {
      payConfig.setMchKey(null);
    }
    return Result.success(payConfigService.updateById(payConfig));
  }

  @ApiOperation(value = "支付配置删除")
  @PreAuthorize("hasAuthority('payapi:payconfig:del')")
  @DeleteMapping("/{id}")
  public Result del(@PathVariable("id") String id) {
    return Result.success(payConfigService.removeById(id));
  }
}
